package com.example.huizhou.controller;

import com.example.huizhou.Service.CourseService;
import com.example.huizhou.Service.SCourseService;
import com.example.huizhou.Service.TeacherService;
import com.example.huizhou.bean.Course;
import com.example.huizhou.bean.Student;
import com.example.huizhou.bean.Teacher;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/teacher")
public class TeacherCotroller {
    int course_id;
    @Resource
    TeacherService teacherService;
    @Resource
    CourseService courseService;
    @Resource
    SCourseService scourseService;

    @RequestMapping("/login")
    public String login(){ return "login"; }

    //教师端主页
    @RequestMapping("/homepage_tc")
    public String homepage_tc(HttpServletRequest request)
    {
        HttpSession session = request.getSession(false);
        return "homepage_tc";
    }
    //个人信息
    @RequestMapping("/教师个人信息")
    public String MSG(HttpServletRequest request, Model model){
        HttpSession session = request.getSession(false);
        int id = Integer.valueOf(session.getAttribute("user_id").toString());
        Teacher teacher = teacherService.findls(id);
        model.addAttribute("Teacher",teacher);
        return "个人信息_tc";
    }
    @RequestMapping("/修改密码")
    @ResponseBody
    public String newpsw(String psw,String psw1,HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(psw.equals("")){
            return "psw";
        }else if(psw1.equals("")){
            return "psw1";
        }else if(!psw.equals(psw1)){
            return "unequal";
        }else{
            int id = Integer.valueOf(session.getAttribute("user_id").toString());
            teacherService.updatepsw(id,psw);
            return "yes";
        }
    }
    @RequestMapping("/tutorial")
    public String tutorial(){
        return "任教课程";}
    @RequestMapping("/ututorial")
    @ResponseBody
    public Object ututorial(HttpServletRequest request, Model model) {
        String classify_id = request.getParameter("classify_id");
        String classify = "";
        if (classify_id.equals("1-1")) {
            classify = "公共课";
        } else if (classify_id.equals("1-2")) {
            classify = "专业课";
        } else if (classify_id.equals("2-1")) {
            classify = "通识课";
        }
        HttpSession session = request.getSession(false);
        int id = Integer.valueOf(session.getAttribute("user_id").toString());
        Teacher teacher = teacherService.findls(id);
        String teacher_name=teacher.getName();
        List<Course> courses = courseService.findcourse_th(classify,teacher_name);
        int c_count=courses.size();
        //  System.out.println(c_count);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("course_th",courses);
        map.put("c_count",c_count);
        return map;
    }
    @RequestMapping("/uc_student")
    @ResponseBody
    public Object uc_student(String course_id){
        this.course_id=Integer.parseInt(course_id);
        List<Student> c_student=scourseService.find_cs(this.course_id);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("count",c_student.size());
        return map;
    }
    @RequestMapping("/c_student")
    public String c_student(Model model){
        List<Student> c_student=scourseService.find_cs(course_id);
        String course_name=courseService.find_cname(course_id);
        model.addAttribute("course_name",course_name);
        model.addAttribute("c_student",c_student);
        return "学生查看";
    }
    @RequestMapping("/up_course")
    public String up_course(){return "发布课程";}
    @RequestMapping("/uup_course")
    @ResponseBody
    public Object uup_course(HttpServletRequest request){
        int cid=Integer.parseInt(request.getParameter("cid"));
        String cname=request.getParameter("cname");
        int clength=Integer.parseInt(request.getParameter("clength"));
        int cscore=Integer.parseInt(request.getParameter("cscore"));
        String cweekf=request.getParameter("cweekf");
        String cweekl=request.getParameter("cweekl");
        String cday=request.getParameter("cday");
        String cjie=request.getParameter("cjie");
        String time=cweekf+"-"+cweekl+"周"+cday+cjie;
        String csite=request.getParameter("csite");
        int cnumber=Integer.parseInt(request.getParameter("cnumber"));
        String classify=request.getParameter("classify");
        String cterm=request.getParameter("cterm");
        HttpSession session = request.getSession(false);
        int id = Integer.valueOf(session.getAttribute("user_id").toString());
        Teacher teacher = teacherService.findls(id);
        String cteacher=teacher.getName();
        //0表示上传成功，1表示课程号重复，2表示此老师已在该时间段发布过课程
        int stat=courseService.up_course(cid,cname,clength,cscore,time,csite,cnumber,classify,cteacher,cterm);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("stat",stat);
        return map;
    }
    @RequestMapping("/modify_course")
    public String modify_course(){return "修改课程";}
    @RequestMapping("/umodify_course")
    @ResponseBody
    public Object umodify_course(HttpServletRequest request){
        Map<String,Object> map=new HashMap<String,Object>();
        int cid=Integer.parseInt(request.getParameter("cid"));
        String cname=request.getParameter("cname");
        int clength=Integer.parseInt(request.getParameter("clength"));
        int cscore=Integer.parseInt(request.getParameter("cscore"));
        String time=request.getParameter("time");
        String csite=request.getParameter("csite");
        int cnumber=Integer.parseInt(request.getParameter("cnumber"));
        String classify=request.getParameter("classify");
        String cteacher=request.getParameter("cteacher");
        String term=request.getParameter("cterm");
        String cterm="";
        if(term.equals("大一上学期")){ cterm="1"; }
        else if(term.equals("大一下学期")){ cterm="2"; }
        else if(term.equals("大二上学期")){ cterm="3"; }
        else if(term.equals("大二下学期")){cterm="4";}
        else if(term.equals("大三上学期")){cterm="5";}
        else if(term.equals("大三下学期")){cterm="6";}
        //0表示修改成功，2表示此老师已在该时间段发布过课程
        int stat=courseService.modify_course(cid,cname,clength,cscore,time,csite,cnumber,classify,cteacher,cterm);
        map.put("stat",stat);
        System.out.println(stat);
        return map;
    }
    @RequestMapping("/delete_course")
    public String delete_course(){
        return "删除课程";
    }
    @RequestMapping("/udelete_course")
    public String udelete_course(HttpServletRequest request){
        int courseid=Integer.parseInt(request.getParameter("courseid"));
        courseService.delete_course(courseid);
        return "删除课程";
    }
}
